class Node:
    def __init__(self, data=None):
        self.data = data
        self.next = None
        self.prev = None


class FIFO:
    def __init__(self, max_length):
        self.head = Node()
        self.tail = Node()
        self.head.next = self.tail
        self.tail.prev = self.head
        self.max_length = max_length

    def __len__(self):
        length = 0
        tmp_ptr = self.head
        while tmp_ptr.next != self.tail:
            length += 1
            tmp_ptr = tmp_ptr.next
        return length

    def isFull(self):
        length = len(l)
        return length in range(0, self.max_length)

    def isNull(self):
        length = len(l)
        return length == 0

    def unshift(self, data):
        if self.isFull():
            new_node = Node()
            new_node.data = data
            prev = self.tail.prev
            prev.next = new_node
            new_node.prev = prev
            self.tail.prev = new_node
            new_node.next = self.tail
            return new_node

    def shift(self):
        if not self.isNull():
            node = self.head.next
            nNode = node.next
            self.head.next = nNode
            nNode.prev = self.head
            self.max_length -= 1
            return node.data

    def __getitem__(self, pos):
        assert isinstance(pos, int)
        length = len(self)
        assert pos in range(0, length)
        tmp_ptr = self.head.next
        while pos > 0:
            tmp_ptr = tmp_ptr.next
            pos -= 1
        return tmp_ptr.data

    def __iter__(self):
        tmp_ptr = self.head.next
        while tmp_ptr != self.tail:
            yield tmp_ptr.data
            tmp_ptr = tmp_ptr.next


def print_iter(l):
    for i in range(0, len(l)):
        print(l[i], end=' ')


def line():
    print()
    print('=========================')


if __name__ == "__main__":
    l = FIFO(5)
    l.unshift(1)
    l.unshift(2)
    l.unshift(3)
    l.unshift(4)
    l.unshift(5)
    print_iter(l)
    line()

    # l.shift()
    # print_iter(l)

    for i in range(0, len(l)):
        l.shift()
        print_iter(l)
        print(l.max_length)
        line()

